package com.gistone.commis.api.restController;

import com.gistone.commis.dto.EditorResult;
import com.gistone.commis.dto.FieldError;
import com.gistone.commis.entity.Community;
import com.gistone.commis.entity.Depart;
import com.gistone.commis.entity.Fund;
import com.gistone.commis.mapper.CommunityMapper;
import com.gistone.commis.util.DataTablePageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangfan on 2017/5/5.
 */
@RestController("apiCommunityController")
@RequestMapping("api/community")
public class CommunityController {

    private static final Logger logger = LoggerFactory.getLogger(CommunityController.class);


    @Autowired
    private CommunityMapper communityMapper;




    /**
     * 获取所有社区
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/all")
    public List<Community> loadAll() {
        return communityMapper.findCommunitys();
    }

    /**
     * 获取所有社区
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/allCom")
    public List<Community> loadAllCom() {
        return communityMapper.selectCommunitysWithout1();
    }



    /**
     * 新建社区信息
     *
     * @param request
     * @param response
     * @return
     */
    @Transactional
    @RequestMapping(method = RequestMethod.POST, value = "/create")
    public Object create(HttpServletRequest request, HttpServletResponse response) {


        Community community = new Community();
        EditorResult<Community> communityEditorResult = new EditorResult<>();
        community.setCommunityName(request.getParameter("data[0][communityName]"));

        community.setIsDelete(0);
        if (communityMapper.insertSelective(community)> 0) {
            List<Community> data = new ArrayList<>();
            data.add(communityMapper.selectByPrimaryKey(community.getCommunityId()))	;
            communityEditorResult.setData(data);
        } else {
            response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        }


        return communityEditorResult;

    }

    /**
     * 批量修改社区信息， id以,号分割
     *
     * @param houseIdStr
     * @param request
     * @return
     */
    @Transactional
    @RequestMapping(method = RequestMethod.PUT, value = "/edit")
    public Object edit(@RequestParam("id") String houseIdStr, HttpServletRequest request, HttpServletResponse response) {
        String[] arr = houseIdStr.split(",");
        EditorResult<Community> communityEditorResult = new EditorResult<>();

        List<Community> data =new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            int communityId = Integer.valueOf(arr[i]);

            System.out.println(communityId);
            Community community = new Community();

            community.setCommunityId(communityId);
            community.setCommunityName(request.getParameter("data["+communityId+"][communityName]"));


            if (communityMapper.updateByPrimaryKeySelective(community) > 0) {
                data.add(communityMapper.selectByPrimaryKey(communityId));


            }else{
                response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            }


        }

        communityEditorResult.setData(data);
        return communityEditorResult;


    }


    /**
     * 用户删除
     * 通过房屋ID批量删除房屋信息， id以,号分割
     *
     * @param communityIdStr
     * @param request
     * @return
     */
    @Transactional
    @RequestMapping(method = RequestMethod.DELETE, value = "/remove")
    public Object remove(@RequestParam("id") String communityIdStr, HttpServletRequest request, HttpServletResponse response) {
        String[] arr = communityIdStr.split(",");
        EditorResult<Community> communityEditorResult = new EditorResult<>();
        for (int i = 0; i < arr.length; i++) {
            int communityId = Integer.valueOf(arr[i]);
            Community c =new Community();
            c.setCommunityId(communityId);
            c.setIsDelete(1);
            if (communityMapper.updateByPrimaryKeySelective(c) > 0) {
                System.out.println("删除" + communityId + "成功！");


            } else {
                response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            }
        }
        return communityEditorResult;


    }

    /**
     * dataTables 表单数据返回
     *
     * @param request
     * @return
     */

    @RequestMapping(method = RequestMethod.GET, value = "/pageDatatables")
    public DataTablePageUtil<Community> pageDatatables(HttpServletRequest request, Community community) {
//		HttpSession httpSession = request.getSession();
//		Community community = (Community) httpSession.getAttribute("communityInfo");
//		logger.info("用户信息：" + community.toString());
//		int community_id = community.getCommunity().getCommunityId();
//		community.setCommunityId(community_id);
        System.out.println(community.toString());


        community.setIsDelete(0);

        DataTablePageUtil<Community> dataTable = new DataTablePageUtil<>(request);

        PageHelper.startPage(dataTable.getPage_num(), dataTable.getPage_size());
        List<Community> list = communityMapper.selectSelective(community);
        PageInfo<Community> pageInfo =new PageInfo<>(list);
        dataTable.setDraw(dataTable.getDraw());
        dataTable.setData(pageInfo.getList());
        dataTable.setRecordsTotal((int)pageInfo.getTotal());
        dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
        //返回数据到页面


        return dataTable;

    }


}
